/**
 * <p>Project: idcenter</p>
 * <p>Copyright: Copyright (c) 2019 daervin.com</p>
 */
package com.daervin.idcenter;

/**
 * 配置项
 *
 * @author zscao
 */
public class BitsConfig {

    public static final int TOTAL_BITS = 64;

    public static final int SIGN_BITS = 1;
    private final int timestampBits;
    private final int workIdBits;
    private final int sequenceBits;

    private final long maxSeconds;
    private final long maxWorkerId;
    private final long maxSequence;

    private final int timestampShift;
    private final int workerIdShift;

    public BitsConfig(int timestampBits, int workIdBits, int sequenceBits) {
        //校验总位数是64
        if (SIGN_BITS + timestampBits + workIdBits + sequenceBits != TOTAL_BITS) {
            throw new RuntimeException("传入的参数总和不合法！");
        }
        //初始化位数
        this.timestampBits = timestampBits;
        this.workIdBits = workIdBits;
        this.sequenceBits = sequenceBits;

        //初始化各标志位的最大值
        this.maxSeconds = (1 << timestampBits) - 1;
        this.maxWorkerId = (1 << workIdBits) - 1;
        this.maxSequence = (1 << sequenceBits) - 1;

        //初始化各标志位需要移位的位数
        this.timestampShift = workIdBits + sequenceBits;
        this.workerIdShift = sequenceBits;
    }

    public int getTimestampBits() {
        return timestampBits;
    }

    public int getWorkIdBits() {
        return workIdBits;
    }

    public int getSequenceBits() {
        return sequenceBits;
    }

    public long getMaxSeconds() {
        return maxSeconds;
    }

    public long getMaxWorkerId() {
        return maxWorkerId;
    }

    public long getMaxSequence() {
        return maxSequence;
    }

    public int getTimestampShift() {
        return timestampShift;
    }

    public int getWorkerIdShift() {
        return workerIdShift;
    }
}
